package com.example.xgakj.newterminal.utils;

import android.graphics.Bitmap;

import com.example.xgakj.mylibrary.mvp.utils.TimeUtil;
import com.example.xgakj.newterminal.base.Parameters;
import com.socks.library.KLog;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017-06-08.
 */

public class FileUtil {
    /**
     * 保存图片到SD卡
     *
     * @param bitmap 图片
     * @param filePath 文件夹路径
     * @param fileName 文件名
     *
     * @throws Exception
     */
    public static boolean savePictureToFile(Bitmap bitmap, String filePath, String fileName) throws Exception {
        File fileFolder = new File(filePath);
        if (!fileFolder.exists()) {
            fileFolder.mkdirs();
        }
        File file = new File(filePath, fileName);
        if (!file.exists()) {
            KLog.d(Parameters.TAG, "file create success ");
            file.createNewFile();
        }
        FileOutputStream out = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 50, out);
        out.flush();
        out.close();
        KLog.d(Parameters.TAG, "file save success ");
        KLog.d(Parameters.TAG, file.getAbsolutePath());
        return true;



    }

    /**
     * 返回视频文件集合
     * @param strPath 文件夹路径
     * @return
     */
    public static List<String> getFiles(final String strPath) {
        List<String> list = new ArrayList<String>();
        File file = new File(strPath);
        File[] allfiles = file.listFiles();
        if (allfiles == null) {
            return null;
        }
        for(int k = 0; k < allfiles.length; k++) {
            final File fi = allfiles[k];
            if(fi.isFile()) {
                int idx = fi.getPath().lastIndexOf(".");
                if (idx <= 0) {
                    continue;
                }
                String suffix = fi.getPath().substring(idx);
                if (suffix.toLowerCase().equals(".h264")|suffix.toLowerCase().equals(".mp4") ) {
                    list.add(fi.getPath());
                }
            }
        }
        return list;
    }
    /**
     * 返回视频文件集合
     * @param strPath 文件夹路径
     * @return
     */
    public static List<String> getPicFiles(final String strPath) {
        List<String> list = new ArrayList<String>();
        File file = new File(strPath);
        File[] allfiles = file.listFiles();
        if (allfiles == null) {
            return null;
        }
        for(int k = 0; k < allfiles.length; k++) {
            final File fi = allfiles[k];
            if(fi.isFile()) {
                int idx = fi.getPath().lastIndexOf(".");
                if (idx <= 0) {
                    continue;
                }
                String suffix = fi.getPath().substring(idx);
                if (suffix.toLowerCase().equals(".png")|suffix.toLowerCase().equals(".jpg") ) {
                    list.add(fi.getPath());
                }
            }
        }
        return list;
    }

    /**
     * 根据参数查询文件
     * @param strPath 文件夹路径
     * @param suffix1 文件类型后缀
     * @param startTime 开始时间
     * @param endTime 截止时间
     * @return 在时间范围内的文件路径集合
     */

    public static List<String> selectFilesByTime(String strPath,String suffix1,String suffix2,long startTime,long endTime,String format) {
        List<String> list = new ArrayList<String>();
        File file = new File(strPath);
        if (!file.exists()){
            return null;
        }
        File[] allfiles = file.listFiles();
        if (allfiles == null) {
            return null;
        }
        for(int k = 0; k < allfiles.length; k++) {
            final File fi = allfiles[k];
            if(fi.isFile()) {
                KLog.e(fi.getPath());
                int id1 = fi.getPath().lastIndexOf("/");
                int id2 = fi.getPath().lastIndexOf(".");
                if (id1<= 0||id2<=0) {
                    continue;
                }
                String suffix = fi.getPath().substring(id2);
                if (suffix.toLowerCase().equals(suffix1)||suffix.toLowerCase().equals(suffix2) ) {
                    String fileName=fi.getPath().substring(id1+1,id2);
                    KLog.e(fileName);
                    long aLong = TimeUtil.Date2ms(fileName,format);
                    if (aLong!=0&&aLong>=startTime&&aLong<=endTime){
                        list.add(fi.getPath());
                    }

                }
            }
        }
        return list;
    }
    public static void deleteFile(String strPath){
        File file=new File(strPath);
        if (file.exists()){
            file.delete();
        }

    }
}
